package com.macrohard.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.macrohard.admin.service.SysMenuService;
import com.macrohard.common.base.response.R;
import com.macrohard.mapper.domain.SysMenu;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("menu")
@Api(tags = "菜单接口")
@RequiredArgsConstructor
public class MenuController {

	private final SysMenuService sysMenuService;

	@GetMapping
	public R getAllMenu() {
		List<SysMenu> allMenu = sysMenuService.getAllMenu();
		return R.ok("ok", allMenu);
	}

	@GetMapping("teacher/{id}")
	public R getMenuWithAccess(@PathVariable("id") Integer id, Boolean flat) {
		if (Objects.isNull(flat)) {
			flat = false;
		}
		return R.ok("ok", sysMenuService.getMenuWithTeacherRole(id, flat));
	}

	@PostMapping
	public R add(@RequestBody SysMenu menu) {
		return R.wrapEffectResult(sysMenuService.save(menu));
	}

	@PutMapping
	public R update(@RequestBody SysMenu menu) {
		return R.wrapEffectResult(sysMenuService.updateById(menu));
	}

}
